package com.sd.web.cti;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.sd.common.utils.cti.XmlMsgParser;
import com.sd.cti.server.cmd.*;
import com.sd.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * cti坐席接口控制器
 */
@RestController
@RequestMapping("/cti")
public class CtiController{

    @Autowired
    private CustomerService customerService;
    /**
     * 坐席登录
     * @param request 请求
     * @param response 响应
     */
  @RequestMapping(value = "/login",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
  public void login(HttpServletRequest request, HttpServletResponse response){
      Engine engine = Engine.use();
      //engine模版渲染
      engine.setDevMode(true);
      engine.setToClassPathSourceFactory();
      try {
           //HttpServletRequest request = getRequest();
           String xml = HttpKit.readData(request);
           System.out.println("xml"+xml);
           AgentLoginCmd agentLoginCmd = XmlMsgParser.parserToModel(xml, AgentLoginCmd.class);
           System.out.println("agentLoginCmd:"+agentLoginCmd.toString());
          Template template = engine.getTemplate("succ.xml");
          customerService.respLogin(response,agentLoginCmd);
          // 字节流模式输出到 OutputStream
          template.render(response.getOutputStream());
      }catch (Exception e){
          e.printStackTrace();
          try {
              Engine failEngine = Engine.create("fail");
              //engine模版渲染
              failEngine.setDevMode(true);
              failEngine.setToClassPathSourceFactory();
              ServletOutputStream outputStream = response.getOutputStream();
              engine.removeAllTemplateCache();
              Template template = failEngine.getTemplate("fail.xml");
              Kv by = Kv.by("exeRemk", "agentId not exist");
              template.render(by,outputStream);
          } catch (IOException ioException) {
              ioException.printStackTrace();
          }
      }
  }

    @RequestMapping(value = "/logout",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void logout(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            AgentLogoutCmd agentLogoutCmd = XmlMsgParser.parserToModel(xml, AgentLogoutCmd.class);
            System.out.println("agentLogoutCmd:"+agentLogoutCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/diacall",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void diacall(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            AgentDiacallCmd agentDiacallCmd = XmlMsgParser.parserToModel(xml, AgentDiacallCmd.class);
            System.out.println("agentDiacallCmd:"+agentDiacallCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/diacall2SL",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void diacall2SL(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            AgentDiacall2SLCmd agentDiacall2SLCmd = XmlMsgParser.parserToModel(xml, AgentDiacall2SLCmd.class);
            System.out.println("agentDiacall2SLCmd:"+agentDiacall2SLCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/getRecordInfo",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void getRecordInfo(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            AgentGetRecordInfoCmd agentGetRecordInfoCmd = XmlMsgParser.parserToModel(xml, AgentGetRecordInfoCmd.class);
            System.out.println("agentGetRecordInfoCmd:"+agentGetRecordInfoCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/downloadRecordsByUUID",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void downloadRecordsByUUID(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            DownloadRecordsByUUIDCmd downloadRecordsByUUIDCmd = XmlMsgParser.parserToModel(xml, DownloadRecordsByUUIDCmd.class);
            System.out.println("downloadRecordsByUUIDCmd:"+downloadRecordsByUUIDCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/queryCallInfo",produces = {"application/xml;charset=UTF-8"},method = RequestMethod.POST)
    public void queryCallInfo(HttpServletRequest request, HttpServletResponse response){
        Engine engine = Engine.use("success");
        //engine模版渲染
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        try {
            //HttpServletRequest request = getRequest();
            String xml = HttpKit.readData(request);
            System.out.println("xml"+xml);
            QueryCallInfoCmd queryCallInfoCmd = XmlMsgParser.parserToModel(xml, QueryCallInfoCmd.class);
            System.out.println("queryCallInfoCmd:"+queryCallInfoCmd.toString());
            Template template = engine.getTemplate("succ.xml");
            ServletOutputStream outputStream = response.getOutputStream();
            // 字节流模式输出到 OutputStream
            template.render(outputStream);
            throw new Exception("1111");
        }catch (Exception e){
            e.printStackTrace();
            try {
                Engine failEngine = Engine.use("fail");
                //engine模版渲染
                failEngine.setDevMode(true);
                failEngine.setToClassPathSourceFactory();
                ServletOutputStream outputStream = response.getOutputStream();
                engine.removeAllTemplateCache();
                Template template = failEngine.getTemplate("fail.xml");
                Kv by = Kv.by("exeRemk", "agentId not exist");
                template.render(by,outputStream);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
